<?php
class Monedas extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library(
                array(
                    'session',
                    'r_session',
                    'form_validation'
                ));
        $this->load->helper(
                array(
                    'url'
                ));
        $this->load->model(
                array(
                    'monedas_model'
                ));
        $this->r_session->check($this->session->all_userdata());
    }
    
    public function index() {
        $session = $this->session->all_userdata();
        $session['title'] = 'Listado de Monedas';
        $data['monedas'] = $this->monedas_model->get_monedas();
        
        $this->load->view('layout/header', $session);
        $this->load->view('monedas/index', $data);
        $this->load->view('layout/footer');
    }
    
    public function update($id) {
        $session = $this->session->all_userdata();
        $session['title'] = 'Modificar Moneda';
        $data['moneda'] = $this->monedas_model->get_moneda_por_id($id);
        
        $this->form_validation->set_rules('moneda', 'Moneda', 'required');
        $this->form_validation->set_rules('simbolo', 'Símbolo', 'required');
        $this->form_validation->set_rules('currency', 'Currency', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'idMoneda' => $id,
                'moneda' => $this->input->post('moneda'),
                'simbolo' => $this->input->post('simbolo'),
                'currency' => $this->input->post('currency')
            );
            
            $this->monedas_model->update($datos, $id);
            
            redirect('/monedas/', 'refresh');
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('monedas/update', $data);
        $this->load->view('layout/footer');
    }
    
    public function agregar() {
        $session = $this->session->all_userdata();
        $session['title'] = 'Crear moneda';
        
        $this->form_validation->set_rules('moneda', 'Moneda', 'required');
        $this->form_validation->set_rules('simbolo', 'Símbolo', 'required');
        $this->form_validation->set_rules('currency', 'Currency', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'moneda' => $this->input->post('moneda'),
                'simbolo' => $this->input->post('simbolo'),
                'currency' => $this->input->post('currency'),
                'habilitado' => 1
            );
            
            $this->monedas_model->set_moneda($datos);
            
            redirect('/monedas/', 'refresh');
        }
        
        $this->load->view('layout/header', $session);
        $this->load->view('monedas/agregar');
        $this->load->view('layout/footer');
    }
}
?>
